// stationDetail.js
const app = getApp()
const util = require('../../utils/util.js')

Page({
  data: {
    lineId: null,
    stationId: null,
    lineInfo: null,
    stationInfo: null,
    nearbyToiletStations: []
  },
  onShareAppMessage() {
    return {
      title: "天津地铁找厕所", // 分享时显示的标题
      path: "/pages/index/index", // 分享的页面路径（必须以 / 开头）
      imageUrl: "/images/分享Logo.png", // 分享卡片的图片（可选，建议5:4比例）
    };
  },
  // 可选：支持“分享到朋友圈”（基础库 2.11.3+ 支持）
  onShareTimeline() {
    return {
      title: "天津地铁找厕所",
      imageUrl: "/images/分享Logo.png",
    };
  },
  
  onLoad: function (options) {
    const lineId = parseInt(options.lineId)
    const stationId = parseInt(options.stationId)
    
    this.setData({
      lineId: lineId,
      stationId: stationId
    })
    
    // 获取线路和站点信息
    const metroLines = app.globalData.metroLines
    const lineInfo = metroLines.find(line => line.id === lineId)
    
    if (lineInfo) {
      const stationInfo = lineInfo.stations.find(station => station.id === stationId)
      
      if (stationInfo) {
        // 获取站点在线路中的索引
        const stationIndex = util.getStationIndexInLine(lineInfo, stationId)
        
        // 如果站点没有厕所，查找附近有厕所的站点
        let nearbyToiletStations = []
        if (!stationInfo.hasToilet && stationIndex !== -1) {
          nearbyToiletStations = util.getNearbyToiletStations(lineInfo, stationIndex)
        }
        
        this.setData({
          lineInfo: lineInfo,
          stationInfo: stationInfo,
          nearbyToiletStations: nearbyToiletStations
        })
        
        // 设置导航栏标题
        wx.setNavigationBarTitle({
          title: stationInfo.name + ' 站'
        })
      } else {
        this.showErrorAndGoBack('未找到站点信息')
      }
    } else {
      this.showErrorAndGoBack('未找到线路信息')
    }
  },
  
  // 显示错误并返回
  showErrorAndGoBack: function(message) {
    wx.showToast({
      title: message,
      icon: 'none'
    })
    setTimeout(() => {
      wx.navigateBack()
    }, 1500)
  },
  
  // 返回线路详情页
  goBack: function() {
    wx.navigateBack()
  }
})